package com.bidianying.util;

import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;




import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.ShardedJedisPool;

public class RedisUtils 
{
	public static final int REDIS_ERROR = 2;
	public static final int KEY_NOTEXISTES = 3;
	public static ShardedJedis getRedis(ShardedJedisPool shardedJedisPool)
	{
		try
		{
		    return shardedJedisPool.getResource();
		}catch(Exception e)
		{
			 return null;
		}
	}
	
	public static String  get(ShardedJedisPool shardedJedisPool,String key)
	{
		ShardedJedis redis  = getRedis(shardedJedisPool);
		if(redis==null)
		{
			return null;
		}
		try
		 {
			return redis.get(key);

		 }catch(Exception e)
		 {
			 return null;
		 }
		 finally
		 {
			 
		     returnResource(shardedJedisPool,redis);
			 
		 }
		
	}
	
	
	public static byte  set(ShardedJedisPool shardedJedisPool,String key,String val,int time)
	{
		ShardedJedis redis  = getRedis(shardedJedisPool);
		if(redis==null)
		{
			return REDIS_ERROR;
		}
		try
		 {
			redis.set(key, val);
			if(time>0)
				redis.expire(key, time);
			
			return 0;
		 }catch(Exception e)
		 {
			 return REDIS_ERROR;
		 }
		 finally
		 {
			 
		    returnResource(shardedJedisPool,redis);
			 
		 }
		
	}
	public static byte hmset(ShardedJedisPool shardedJedisPool,String key,Map<String,String> map,int time)
	{
		ShardedJedis redis  = getRedis(shardedJedisPool);
		if(redis==null)
		{
			return REDIS_ERROR;
		}
		try
		 {
			redis.hmset(key, map);
			if(time>0)
				redis.expire(key, time);
			
			return 0;
		 }catch(Exception e)
		 {
			 return REDIS_ERROR;
		 }
		 finally
		 {
			 
		    returnResource(shardedJedisPool,redis);
			 
		 }
		
	}
	

	
	
	
	
	
	public static byte hgetAll(ShardedJedisPool shardedJedisPool,String key,Map<String,String> map)
	{
		ShardedJedis redis  = getRedis(shardedJedisPool);
		if(redis==null)
		{
			return REDIS_ERROR;
		}
		try
		 {
			
			map.putAll(redis.hgetAll(key));
			return 0;
		 }catch(Exception e)
		 {
			 return REDIS_ERROR;
		 }
		 finally
		 {
			 
			returnResource(shardedJedisPool,redis);
			 
		 }
		
	}
	
	public static Set<String> smembers(ShardedJedisPool shardedJedisPool,String key)
	{
		ShardedJedis redis  = getRedis(shardedJedisPool);
		if(redis==null)
			return null;
		try
		 {
			return redis.smembers(key);
		 }catch(Exception e)
		 {
			 return null;
		 }
		 finally
		 {
			 
			returnResource(shardedJedisPool,redis);
			 
		 }
		
	}
	
	public static List<String> hmget(ShardedJedisPool shardedJedisPool,String key,String[] fields)
	{
		ShardedJedis redis  = getRedis(shardedJedisPool);
		if(redis==null)
			return null;
		try
		 {
			return redis.hmget(key, fields);
		 }catch(Exception e)
		 {
			 return null;
		 }
		 finally
		 {
			 returnResource(shardedJedisPool,redis);
			 
		 }
		
	}
	
	public static Set<String> hkeys(ShardedJedisPool shardedJedisPool,String key)
	{
		ShardedJedis redis  = getRedis(shardedJedisPool);
		if(redis==null)
			return null;
		try
		 {
			return redis.hkeys(key);
		 }catch(Exception e)
		 {
			 return null;
		 }
		 finally
		 {
			 returnResource(shardedJedisPool,redis);
			 
		 }
		
	}
	public static long hlen(ShardedJedisPool shardedJedisPool,String key)
	{
		ShardedJedis redis  = getRedis(shardedJedisPool);
		if(redis==null)
			return  -1;
		try
		 {
			return redis.hlen(key);
		 }catch(Exception e)
		 {
			 return  -1;
		 }
		 finally
		 {
			 returnResource(shardedJedisPool,redis);
			 
		 }
		
	}
	
	
	
	public static String hget(ShardedJedisPool shardedJedisPool,String key,String field)
	{
		ShardedJedis redis  = getRedis(shardedJedisPool);
		if(redis==null)
			return null;
		try
		 {
			return redis.hget(key, field);
		 }catch(Exception e)
		 {
			 return null;
		 }
		 finally
		 {
			
			 returnResource(shardedJedisPool,redis);
			 
		 }
		
	}
	
	
	public static byte hset(ShardedJedisPool shardedJedisPool,String key,String field,String val)
	{
		ShardedJedis redis  = getRedis(shardedJedisPool);
		if(redis==null)
			 return REDIS_ERROR;;
		try
		 {
			redis.hset(key, field,val);
			return 0;
		 }catch(Exception e)
		 {
			 return REDIS_ERROR;
		 }
		 finally
		 {
			 
			 returnResource(shardedJedisPool,redis);
			 
		 }
		
	}
	
	
	public static byte exists(ShardedJedisPool shardedJedisPool,String key)
	{
		ShardedJedis redis  = getRedis(shardedJedisPool);
		if(redis==null)
			return REDIS_ERROR;
		
		
		try
		 {
			if(redis.exists(key))
			  return (byte)0;
			else
			  return KEY_NOTEXISTES;
		 }catch(Exception e)
		 {
			 return REDIS_ERROR;
		 }
		 finally
		 {
			returnResource(shardedJedisPool,redis); 
		 }

	}
	
	public static byte del(ShardedJedisPool shardedJedisPool,String key)
	{
		ShardedJedis redis  = getRedis(shardedJedisPool);
		if(redis==null)
			return REDIS_ERROR;
		
		
		try
		 {
			redis.del(key);
			return 0;
		 }catch(Exception e)
		 {
			 return REDIS_ERROR;
		 }
		 finally
		 {
		   returnResource(shardedJedisPool,redis);	 
		 }

	}
	
	public static byte hdel(ShardedJedisPool shardedJedisPool,String key,String field)
	{
		ShardedJedis redis  = getRedis(shardedJedisPool);
		if(redis==null)
			return REDIS_ERROR;
		
		
		try
		 {
			redis.hdel(key,field);

			return 0;
		 }catch(Exception e)
		 {
			 return REDIS_ERROR;
		 }
		 finally
		 {
		   returnResource(shardedJedisPool,redis);	 
		 }

	}
	
	public static byte hexists(ShardedJedisPool shardedJedisPool,String key,String field)
	{
		ShardedJedis redis  = getRedis(shardedJedisPool);
		if(redis==null)
			return REDIS_ERROR;
		
		
		try
		 {
			return redis.hexists(key, field)?(byte)0:(byte)1;
			
		 }catch(Exception e)
		 {
			 return REDIS_ERROR;
		 }
		 finally
		 {
		   returnResource(shardedJedisPool,redis);	 
		 }

	}
	

	
	public static byte expire(ShardedJedisPool shardedJedisPool,String key,int time)
	{
		ShardedJedis redis  = getRedis(shardedJedisPool);
		if(redis==null)
			return REDIS_ERROR;

		try
		 {
			redis.expire(key, time);
			return 0;
		 }catch(Exception e)
		 {
			 return REDIS_ERROR;
		 }
		 finally
		 {
		   returnResource(shardedJedisPool,redis);	 
		 }

	}
	
	
	public static byte zadd(ShardedJedisPool shardedJedisPool,String key,String field,int score,int time)
	{
		ShardedJedis redis  = getRedis(shardedJedisPool);
		if(redis==null)
			return REDIS_ERROR;
		
		
		try
		 {
			redis.zadd(key, score,field);
			redis.expire(key, time);
			
			return 0;
			
		 }catch(Exception e)
		 {
			 return REDIS_ERROR;
		 }
		 finally
		 {
		   returnResource(shardedJedisPool,redis);	 
		 }

	}
	
	public static Set<String> zrangeByScore(ShardedJedisPool shardedJedisPool,String key,double min,double max,int offset,int count)
	{
		ShardedJedis redis  = getRedis(shardedJedisPool);
		if(redis==null)
			return null;
		try
		 {
			return redis.zrangeByScore(key, min, max, offset, count);

		 }catch(Exception e)
		 {
			 return null;
		 }
		 finally
		 {
		   returnResource(shardedJedisPool,redis);	 
		 }

	}
	/**
	 * 
	 * @param shardedJedisPool
	 * @param key
	 * @param sortSet
	 * @param time
	 * @param flag   0   1:无差别删除数据
	 * @return
	 */
	public static byte batchZadd(ShardedJedisPool shardedJedisPool,String key,Map<String,Double> sortSet,int time,int flag)
	{
		ShardedJedis redis  = getRedis(shardedJedisPool);
		if(redis==null)
			return REDIS_ERROR;
		try
		 {
			Set<String>  nowSet = new HashSet<String>();
			for(String field:sortSet.keySet())
			{
				  redis.zadd(key, sortSet.get(field), field);
				  if(flag==1)
				  {
					  nowSet.add(field);
				  }
			}
			if(flag==1)
			{
				Set<String>  oldSet = redis.zrange(key, 0, -1);
				Set<String> comIds = compSetIndexs(nowSet, oldSet);
				if(comIds==null||comIds.size()==0)
				{
					return 0;
				}
				for(String member:comIds)
				{
					redis.zrem(key, member);
				}
				
			}
			return 0;
		 }catch(Exception e)
		 {
			 return REDIS_ERROR;
		 }
		 finally
		 {
		   returnResource(shardedJedisPool,redis);	 
		 }

	}
	
	public static long  zcard(ShardedJedisPool shardedJedisPool,String key)
	{
		ShardedJedis redis  = getRedis(shardedJedisPool);
		if(redis==null)
		{
			return -1;
		}
		try
		 {
				return redis.zcard(key);
			
		 }catch(Exception e)
		 {
			 return -1;
		 }
		 finally
		 {
			 
		    returnResource(shardedJedisPool,redis);
			 
		 }
		
	}
	public static Set<String> compSetIndexs(Set<String> set1,Set<String> set2)
	{
		if(set1==null || set1.size()==0)
			return set2;
		if(set2==null)
			return null;

         Set<String> comSet = new HashSet<String>();
         for(String id:set2)
         {
        	 if(!set1.contains(id))
        	 {
        		 comSet.add(id);
        	 }
         }
         return comSet;
	}
	
	public static void returnResource(ShardedJedisPool shardedJedisPool,ShardedJedis redis)
	{
		try
		{
		  if(redis!=null)
		    shardedJedisPool.returnResource(redis);
		}catch(Exception e)
		{
			e.printStackTrace();
		}
	}

}
